<?php
/**
 * Controller plugin that sets the correct paths to the Zend_Layout instances
 * 
 * @uses       Zend_Controller_Plugin_Abstract
 * 
 * @category   Core
 * @package    Core_Controller
 * @subpackage Plugin
 * 
 * @version  $Id$
 */
class Core_Controller_Plugin_Navigation extends Zend_Controller_Plugin_Abstract
{
    /**
     * Plugin configuration settings array
     *
     * @var array
     */
    protected $_options = array();

    /**
     * Constructor
     *
     * Options may include:
     * - config
     *
     * @param  Array $options
     * @return void
     */
    public function __construct(Array $options = array())
    {
        $this->_options = $options;
    }
    
    /**
     * preDispatch
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $this->_initNavigation();
    }

    
    /**
     * _initNavigation
     * 
     * @param   string $section
     * @return  void  
     */
    protected function _initNavigation() 
    {
        $container = new Zend_Navigation($this->_getConfig());
        
        Zend_Layout::getMvcInstance()->getView()->navigation($container);
        
        try {
            $acl = Zend_Registry::get('Zend_Acl');
            $translator = Zend_Registry::get('Zend_Translate');
 
            $identity = Zend_Auth::getInstance()->getIdentity();
            $role = $identity ? $identity->role : 'guest';
                
            Zend_Layout::getMvcInstance()->getView()->navigation()
                                         ->setAcl($acl)
                                         ->setRole($role)
                                         ->setTranslator($translator);
        } catch (Exception $e) {
            // Acl is not inited            
        }
    }
    
    /**
     * get config
     *
     * @param  string
     * @return array
     */
    protected function _getConfig()
    {
        if (Zend_Registry::get('Zend_Cache_Manager')->hasCache('navigation')) {
            $cache = Zend_Registry::get('Zend_Cache_Manager')->getCache('navigation');
            Core_Module_Config::setCache($cache);
        }
        $moduleConfig = new Core_Module_Config($this->_options);
        $config = $moduleConfig->readConfigs();
        return $config;
    }
}